<?php
/**
 * Created by PhpStorm.
 * User: serafina
 * Date: 2015/9/23
 * Time: 09:50
 */

function mobile_service_revisit_list($parameters){
    global $user;
    $query = db_select('revisit', 'R')->extend('PagerDefault')->extend('TableSort');
    $query->join('farmer', 'F', 'F.farmer_id=R.farmer_id');
    $query->join('plant', 'P', 'P.plant_id=R.plant_id');
    $query->join('addr', 'A', 'A.addr_id=F.addr_id');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->leftJoin('addr', 'A4', 'A.town_id=A4.addr_id');
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    $query->addField('A4', 'name', 'town');
    $query->addField('A', 'name', 'village');
    if(!user_access('view all revisit') && user_access('view own revisit')){
        $query->condition('R.author_uid',$user->uid, '=');
    }
    //地址级别查询
    if (!empty($parameters['village'])) {//村落级
        $query->condition('A.village_id', $parameters['village'], '=');
    } else if (!empty($parameters['town'])) {//乡镇级
        $query->condition('A.town_id', $parameters['town'], '=');
    } elseif (!empty($parameters['county'])) {//县级
        $query->condition('A.county_id', $parameters['county'], '=');
    } elseif (!empty($parameters['city'])) {//市级
        $query->condition('A.city_id', $parameters['city'], '=');
    } elseif (!empty($parameters['province'])) {//省级
        $query->condition('A.province_id', $parameters['province'], '=');
    }
    if (!empty($parameters['plant'])) {//村落级
        $query->condition('R.plant_id', $parameters['plant'], '=');
    }
    if (!empty($parameters['farmer_id'])) {//
        $query->condition('R.farmer_id', $parameters['farmer_id'], '=');
    }

    $datestr = '';
    if (!empty($parameters['year'])) {//查询条件
        if ($parameters['year']) {
            $datestr .= $parameters['year'] . '-';
        }
    }
    if (!empty($parameters['month'])) {//查询条件
        if ($month = $parameters['month']) {
            if ($month < 10)
                $datestr .= '0' . $parameters['month'];
            else
                $datestr .= $parameters['month'];
        }
    }
    if(!empty($parameters['day'])){
        if ($day = $parameters['day']) {
            if ($day < 10)
                $datestr .= '-0' . $parameters['day'];
            else
                $datestr .='-'.$parameters['day'];
        }
    }
    if ($datestr) {
        $query->condition('R.visit_date', db_like($datestr) . '%', 'LIKE');
    }

    $query->fields('R');
    $query->addField('F', 'farmer_name');
    $query->addField('F', 'addr_id');
    $query->addField('P', 'plant_name');
    $query->orderBy('R.revisit_id', 'DESC');
    if (!empty($parameters['cursor']) && !empty($parameters['comparator'])) { //游标翻页
        $query->condition('R.revisit_id', $parameters['cursor'], $parameters['comparator']);
    }
    $query->range(0,10);

    $results = $query->execute();
    $revisit_list = [];
    if(!empty($results)){
        foreach ($results as $result) {
            $revisit_list[] = $result;
        }
    }
    return  ["data"=>$revisit_list];
}

function mobile_service_revisit_add($parameters){
    global $user;
    $module_name="revisit";
    $id = !empty($parameters['revisit_id'])?$parameters['revisit_id']:'';
    if (empty($id)) { //add new brand_sales
        if(!user_access('add '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_insert('revisit') // Table name no longer needs {}
        ->fields(array(
            'visit_date' => $parameters['visit_date'],
            'farmer_id' => $parameters['farmer_id'],
            'plant_id' => $parameters['plant_id'],
            'unit_yield' => $parameters['unit_yield'],
            'net_income' => $parameters['net_income'],
            'contrast_yield' => $parameters['contrast_yield'],
            'contrast_income' => $parameters['contrast_income'],
            'content' => $parameters['content'],
            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->execute();
            $result_option = (object)["option" => 1];
            return $result_option;
    } else {
        if(!mobile_user_edit_access($module_name,$module_name.'_id',$id,'edit own '.$module_name,'edit all '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_update('revisit')
        ->fields(array(
            'visit_date' => $parameters['visit_date'],
            'farmer_id' => $parameters['farmer_id'],
            'plant_id' => $parameters['plant_id'],
            'unit_yield' => $parameters['unit_yield'],
            'net_income' => $parameters['net_income'],
            'contrast_yield' => $parameters['contrast_yield'],
            'contrast_income' => $parameters['contrast_income'],
            'content' => $parameters['content'],
            'timestamp' => time(),))
            -> condition('revisit_id',$id, '=')
            ->execute();
        if($nid > 0){
            $result_option = (object)["option" => 1];
            return $result_option;
        }
        $result_option = (object)["option" => 0];
        return $result_option;
    }
}
